package com.bussiness.system.service;

import com.bussiness.system.dao.IkHotWordsDao;
import com.bussiness.system.model.IkHotWords;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @description: es Service
 * @author: zyj
 * @create: 2019-12-11 17:26
 **/
@Service
public class EsTestService {

    private final String[] articles = {"深化","四川","全国","乡村","天空","好人"};

    private final String[] contexts = {"四川成都的天府软件园","好人","开展实时的测试+","，中选会11日在官网上发出公告，公布领导人候选人的三次以及副领导人候选人一次的政见发表会","spring-cloud","模拟"};

    private final String[] auths = {"张","李","王","慕容","茜","宏"};

    private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private IkHotWordsDao ikHotWordsDao;

    public String getArticle(){
        int i = (int)(Math.random()*6);
        int j = (int)(Math.random()*6);
        return articles[i]+articles[j];
    }

    public String getContext(){
        int i = (int)(Math.random()*6);
        int j = (int)(Math.random()*6);
        return contexts[i]+contexts[j];
    }

    public String getAuth(){
        int i = (int)(Math.random()*6);
        return auths[i]+i;
    }

    public String getRandomTime(){
        Date date = randomDate("1990-01-01","2019-12-31");
        return simpleDateFormat.format(date);
    }

    private static Date randomDate(String beginDate,String endDate){
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date start = format.parse(beginDate);
            Date end = format.parse(endDate);

            if(start.getTime() >= end.getTime()){
                return null;
            }
            long date = random(start.getTime(),end.getTime());
            return new Date(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static long random(long begin,long end){
        long rtn = begin + (long)(Math.random() * (end - begin));
        if(rtn == begin || rtn == end){
            return random(begin,end);
        }
        return rtn;
    }

    /**
    * @Description:
    * @Param: [word]
    * @return: void
    * @Author: zyj
    * @Date: 2019/12/25
    */
    public void saveIkHot(String word) throws Exception{
        IkHotWords ikHotWords = new IkHotWords();
        ikHotWords.setDescription(word);
        ikHotWords.setIkWord(word);
        ikHotWords.setIkStatus("1");
        ikHotWords.setCreateTime(new Date());

        ikHotWordsDao.insert(ikHotWords);


    }

    public void update(IkHotWords ikHotWords)throws Exception{
        ikHotWordsDao.updateByPrimaryKey(ikHotWords);
    }

    public List<IkHotWords> ikHotWordsList()throws Exception{
        return ikHotWordsDao.listIkHot();
    }
}
